@ECHO OFF
SETLOCAL

REM "%SystemRoot%\System32\chcp.com" 65001 > NUL

SET CURRENT_DIR=%~dp0
SET SOURCE=%CURRENT_DIR%conf\init.gradle
SET TARGET=%USERPROFILE%\.gradle\init.gradle

IF DEFINED GRADLE_USER_HOME (
    GOTO process_gradle_user_home_env
) ELSE (
    GOTO backup_distribute
)

:process_gradle_user_home_env
REM remove quotes for %GRADLE_USER_HOME%
SET "TARGET_PREFIX=%GRADLE_USER_HOME:"=%"

:remove_blank
REM remove right blank for %TARGET_PREFIX%
IF "%TARGET_PREFIX:~-1%"==" " (
    SET "TARGET_PREFIX=%TARGET_PREFIX:~0,-1%"
    GOTO remove_blank
)

IF NOT "%TARGET_PREFIX:~-1%"=="\" (
    SET TARGET_PREFIX=%TARGET_PREFIX%\
)

SET TARGET="%TARGET_PREFIX%init.gradle"

:backup_distribute
IF EXIST "%TARGET%" (
    REM backup old
    COPY /Y "%TARGET%" "%TARGET%.bak" > NUL
)

COPY /Y "%SOURCE%" "%TARGET%"

PAUSE > NUL
